/*  
    idf_component.yml文件：
    dependencies:
        idf: ">=5.1"
        lvgl/lvgl: ">=8.*"
        esp_lvgl_port: "^1"
        espressif/esp_lcd_st7796: "^1.0.0"  ==》添加这些
    
    // 该库适用于ST7796多管脚屏幕,如果只使用mosi,可以使用ST7789驱动
*/


#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "driver/gpio.h"
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "esp_lcd_panel_ops.h"
#include "esp_lcd_panel_io.h"

#include "esp_lvgl_port.h" // esp lvgl port组件
#include "esp_lcd_st7796.h"

#include "lvgl.h"

#define _LCD_H_RES              (320)
#define _LCD_V_RES              (480)
#define _LCD_BIT_PER_PIXEL      (16)

#define LCD_DATA_WIDTH          (8)

#define PIN_NUM_LCD_DC          (4)
#define PIN_NUM_LCD_WR          (6)
#define PIN_NUM_LCD_CS          (5)
#define PIN_NUM_LCD_RST         (48)

#define PIN_NUM_LCD_DATA0      (GPIO_NUM_9)
#define PIN_NUM_LCD_DATA1      (GPIO_NUM_12)
#define PIN_NUM_LCD_DATA2      (GPIO_NUM_11)
#define PIN_NUM_LCD_DATA3      (GPIO_NUM_14)
#define PIN_NUM_LCD_DATA4      (GPIO_NUM_13)
#define PIN_NUM_LCD_DATA5      (GPIO_NUM_47)
#define PIN_NUM_LCD_DATA6      (GPIO_NUM_21)
#define PIN_NUM_LCD_DATA7      (GPIO_NUM_45)
#define PIN_NUM_LCD_DATA8      (-1)
#define PIN_NUM_LCD_DATA9      (-1)
#define PIN_NUM_LCD_DATA10     (-1)
#define PIN_NUM_LCD_DATA11     (-1)
#define PIN_NUM_LCD_DATA12     (-1)
#define PIN_NUM_LCD_DATA13     (-1)
#define PIN_NUM_LCD_DATA14     (-1)
#define PIN_NUM_LCD_DATA15     (-1)


static char *TAG = "st7796_test";

void st7796_init(void)
{
    ESP_LOGI(TAG, "Initialize Intel 8080 bus");
    esp_lcd_i80_bus_handle_t i80_bus = NULL;
    esp_lcd_i80_bus_config_t bus_config = ST7796_PANEL_BUS_I80_CONFIG(
            _LCD_H_RES * _LCD_V_RES * _LCD_BIT_PER_PIXEL / 8, LCD_DATA_WIDTH,
            PIN_NUM_LCD_DC, PIN_NUM_LCD_WR,
            PIN_NUM_LCD_DATA0, PIN_NUM_LCD_DATA1, PIN_NUM_LCD_DATA2, PIN_NUM_LCD_DATA3,
            PIN_NUM_LCD_DATA4, PIN_NUM_LCD_DATA5, PIN_NUM_LCD_DATA6, PIN_NUM_LCD_DATA7,
            PIN_NUM_LCD_DATA8, PIN_NUM_LCD_DATA9, PIN_NUM_LCD_DATA10, PIN_NUM_LCD_DATA11,
            PIN_NUM_LCD_DATA12, PIN_NUM_LCD_DATA13, PIN_NUM_LCD_DATA14, PIN_NUM_LCD_DATA15);
    ESP_ERROR_CHECK(esp_lcd_new_i80_bus(&bus_config, &i80_bus));

    ESP_LOGI(TAG, "Install panel IO");
    esp_lcd_panel_io_handle_t io_handle = NULL;
    esp_lcd_panel_io_i80_config_t io_config = ST7796_PANEL_IO_I80_CONFIG(PIN_NUM_LCD_CS, NULL, NULL);
    ESP_ERROR_CHECK(esp_lcd_new_panel_io_i80(i80_bus, &io_config, &io_handle));

    ESP_LOGI(TAG, "Install ST7796 panel driver");
    esp_lcd_panel_handle_t panel_handle = NULL;

    const esp_lcd_panel_dev_config_t panel_config = {
        .reset_gpio_num = PIN_NUM_LCD_RST,      // Set to0 if not use
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)      // Implemented by LCD command `36h`
        .color_space = ESP_LCD_COLOR_SPACE_RGB,
#else
        .rgb_endian = LCD_RGB_ENDIAN_RGB,
#endif
        .bits_per_pixel = _LCD_BIT_PER_PIXEL,    // Implemented by LCD command `3Ah` (16/18/24)
    };

    // 初始化esp lvgl port组件
    const lvgl_port_cfg_t lvgl_cfg = {
        .task_priority = 5,       /*!< LVGL 线程优先级 */
        .task_stack = 4096,       /*!< LVGL 线程栈大小 */
        .task_affinity =0,      /*!< LVGL 线程在哪个核心上运行（cpu0或者cpu1）-1为没有要求 */
        .task_max_sleep_ms = 500, /*!< LVGL 线程最大睡眠时间 */
        .timer_period_ms = 5,
    };
    lvgl_port_init(&lvgl_cfg);

    ESP_ERROR_CHECK(esp_lcd_new_panel_st7796(io_handle, &panel_config, &panel_handle));
    ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
    ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
    
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
    ESP_ERROR_CHECK(esp_lcd_panel_disp_off(panel_handle, false));
#else
    ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
#endif
}









